//
//  ChangeLoginPasswordViewController.m
//  1gene
//
//  Created by 1jiyin on 16/3/23.
//  Copyright © 2016年 1jiyin. All rights reserved.
//
//**********修改登录密码**********
#import "ChangeLoginPasswordViewController.h"
#import "AFNetWorking.h"
@interface ChangeLoginPasswordViewController ()
@property(nonatomic,copy)UITextField *userTextField;
@property(nonatomic,copy)UITextField *oldPasswordTextField;
@property(nonatomic,copy)UITextField *myNewPasswordTextField;
@property(nonatomic,copy)UITextField *sureNewPasswordTextField;
@end

@implementation ChangeLoginPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    [self createNavigation];
    [self createUI];
    // Do any additional setup after loading the view.
}
-(void)createUI
{
    
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    NSString *phone=[user objectForKey:@"userPhone"];
    
    //用户账号
    UITextField *userTextField=[[UITextField alloc]initWithFrame:CGRectMake(10, 20+44+20, self.view.frame.size.width-10*2, 40)];
    userTextField.leftViewMode=UITextFieldViewModeAlways;
    UIView *v1=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
    v1.backgroundColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1];
    UIImageView *userLeftView=[[UIImageView alloc]initWithFrame:CGRectMake(5, 4, 30, 32)];
   // userLeftView.backgroundColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1];
    userLeftView.image=[UIImage imageNamed:@"telephone.png"];
    [v1 addSubview:userLeftView];
    userTextField.leftView=v1;
    userTextField.placeholder=phone;
    userTextField.font=[UIFont systemFontOfSize:15];
    userTextField.textColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1];
    userTextField.backgroundColor=[UIColor whiteColor];
    userTextField.layer.borderColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1].CGColor;
    userTextField.layer.borderWidth=1.0;
    userTextField.layer.cornerRadius=5.0;
    userTextField.keyboardType=UIKeyboardTypeNumberPad;
    userTextField.userInteractionEnabled=NO;
    [self.view addSubview:userTextField];
    
    
    //原始密码
    UITextField *oldPasswordTextField=[[UITextField alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(userTextField.frame)+10, self.view.frame.size.width-10*2, 40)];
    oldPasswordTextField.leftViewMode=UITextFieldViewModeAlways;
    UIView *v2=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
    v2.backgroundColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1];
    UIImageView *oldPasswordLeftView=[[UIImageView alloc]initWithFrame:CGRectMake(5, 4, 30, 32)];
    //oldPasswordLeftView.backgroundColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1];
    oldPasswordLeftView.image=[UIImage imageNamed:@"password-3-2.png"];
    [v2 addSubview:oldPasswordLeftView];
    oldPasswordTextField.leftView=v2;
    oldPasswordTextField.placeholder=@"原始密码";
    oldPasswordTextField.font=[UIFont systemFontOfSize:15];
    oldPasswordTextField.textColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1];
    oldPasswordTextField.backgroundColor=[UIColor whiteColor];
    oldPasswordTextField.layer.borderColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1].CGColor;
    oldPasswordTextField.layer.borderWidth=1.0;
    oldPasswordTextField.layer.cornerRadius=5.0;
    [self.view addSubview:oldPasswordTextField];
    
    //新密码
    UITextField *newPasswordTextField=[[UITextField alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(oldPasswordTextField.frame)+10, self.view.frame.size.width-10*2, 40)];
    newPasswordTextField.leftViewMode=UITextFieldViewModeAlways;
    UIView *v3=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
    v3.backgroundColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1];;
    UIImageView *newPasswordLeftView=[[UIImageView alloc]initWithFrame:CGRectMake(5, 4, 30, 32)];
    //newPasswordLeftView.backgroundColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1];
    newPasswordLeftView.image=[UIImage imageNamed:@"password-3-2.png"];
    [v3 addSubview:newPasswordLeftView];
    newPasswordTextField.leftView=v3;
    newPasswordTextField.placeholder=@"新密码";
    newPasswordTextField.font=[UIFont systemFontOfSize:15];
    newPasswordTextField.textColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1];
    newPasswordTextField.backgroundColor=[UIColor whiteColor];
    newPasswordTextField.layer.borderColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1].CGColor;
    newPasswordTextField.layer.borderWidth=1.0;
    newPasswordTextField.layer.cornerRadius=5.0;
    [self.view addSubview:newPasswordTextField];
    
    
    //确认新密码
    UITextField *sureNewPasswordTextField=[[UITextField alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(newPasswordTextField.frame)+10, self.view.frame.size.width-10*2, 40)];
    sureNewPasswordTextField.leftViewMode=UITextFieldViewModeAlways;
    UIView *v4=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
    v4.backgroundColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1];;
    UIImageView *sureNewPasswordLeftView=[[UIImageView alloc]initWithFrame:CGRectMake(5, 4, 30, 32)];
    //sureNewPasswordLeftView.backgroundColor=[UIColor colorWithRed:87/255.0 green:187/255.0 blue:215/255.0 alpha:1];
    sureNewPasswordLeftView.image=[UIImage imageNamed:@"password-3-2.png"];
    [v4 addSubview:sureNewPasswordLeftView];
    sureNewPasswordTextField.leftView=v4;
    sureNewPasswordTextField.placeholder=@"确认新密码";
    sureNewPasswordTextField.font=[UIFont systemFontOfSize:15];
    sureNewPasswordTextField.textColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1];
    sureNewPasswordTextField.backgroundColor=[UIColor whiteColor];
    sureNewPasswordTextField.layer.borderColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1].CGColor;
    sureNewPasswordTextField.layer.borderWidth=1.0;
    sureNewPasswordTextField.layer.cornerRadius=5.0;
    [self.view addSubview:sureNewPasswordTextField];
    
    //确认修改
    UIButton *changeBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    changeBtn.frame=CGRectMake((self.view.frame.size.width-150)/2.0, CGRectGetMaxY(sureNewPasswordTextField.frame)+20, 150, 35);
    [changeBtn setTitle:@"确认" forState:UIControlStateNormal];
    [changeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    changeBtn.titleLabel.font=[UIFont systemFontOfSize:20];
    changeBtn.backgroundColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1];
    changeBtn.layer.borderColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1].CGColor;
    changeBtn.layer.borderWidth=2.0;
    changeBtn.layer.cornerRadius=5.0;
    [changeBtn addTarget:self action:@selector(changeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:changeBtn];
    
    _userTextField=userTextField;
    _oldPasswordTextField=oldPasswordTextField;
    _myNewPasswordTextField=newPasswordTextField;
    _sureNewPasswordTextField=sureNewPasswordTextField;

}
//确认修改
-(void)changeBtnClick:(UIButton *)sender
{
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    NSString *password=[user objectForKey:@"userPassword"];
    if (_oldPasswordTextField.text.length>0) {
        if ([password isEqualToString:_oldPasswordTextField.text]) {
            if ([_myNewPasswordTextField.text isEqualToString:_sureNewPasswordTextField.text]) {
                [self changePassword];
            }else{
                [self.view showTiShiMessage:@"两次输入的密码不一致"];
            }
        }else{
            [self.view showTiShiMessage:@"密码错误"];
        }
        
    }else{
        [self.view showTiShiMessage:@"密码不能为空"];
    }
}
-(void)createNavigation
{
    self.navigationController.navigationBar.barTintColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1];
    //返回
    UIButton *leftBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame=CGRectMake(0, 0, 25, 25);
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"left_back"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(leftBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    
    //标题
    UILabel *title=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];
    title.text=@"修改登录密码";
    title.font=[UIFont systemFontOfSize:22];
    title.textAlignment=NSTextAlignmentCenter;
    title.textColor=[UIColor whiteColor];
    self.navigationItem.titleView=title;
}
//返回上一页
-(void)leftBtnClick:(UIButton *)sender
{
    self.navigationController.navigationBar.hidden=YES;
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.navigationController.view endEditing:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)changePassword
{
    AFHTTPRequestOperationManager *manger=[AFHTTPRequestOperationManager manager];
    manger.requestSerializer=[AFHTTPRequestSerializer serializer];
    manger.responseSerializer=[AFHTTPResponseSerializer serializer];
    
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    NSString *token=[user objectForKey:@"token"];
    
    NSDictionary *params=@{@"password":_oldPasswordTextField.text,@"newpassword":_myNewPasswordTextField.text,@"newpasswordcomfired":_sureNewPasswordTextField.text};
    
    [self.view showCicle:YES];
    [manger POST:[NSString stringWithFormat:ChangeLoginPassword,token] parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self.view showCicle:NO];
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"changePassword==%@",dic);
        [self.view showTiShiMessage:@"修改成功"];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self.view showCicle:NO];
        NSLog(@"error%@",error.localizedDescription);
        [self.view showTiShiMessage:@"密码错误"];
    }];
}

//-(void)viewWillAppear:(BOOL)animated
//{
//    [self.navigationController.navigationBar setTranslucent:YES];
//}
//
//-(void)viewWillDisappear:(BOOL)animated
//{
//    [self.navigationController.navigationBar setTranslucent:NO];
//}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
